{include file="common/header"}
<style>
    .layui-input-affix {
        line-height: 34px;
    }
</style>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <div class="layui-input-group" style="width: 90% !important;margin-left: 10%;">
                    <input type="text" placeholder="搜索题库内容" class="layui-input" lay-filter="search"
                           lay-affix="search" lay-options="{split: true}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">*</font>问题名称</label>
                <div class="layui-input-block">
                    <select name="qid" id="question" lay-filter="question" lay-search>
                        {foreach $questionList as $k=>$v}
                        <option value="{$v.id}" {if isset($data[
                        'qid'])&&$v.id==$data['qid']}selected="selected"{/if}>{$v.title}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">*</font>问题分数</label>
                <div class="layui-input-block">
                    <input type="number" name="score" min="1" class="layui-input" lay-verify="required"
                           value="{$data['score']??'1'}">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <input type="hidden" name="__token__" id="__token__" value="{:token()}"/>
            <input type="hidden" name="id" value="{$data['id']??''}">
            <input type="hidden" name="pid" value="{$pid}">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script>
    layui.use(['form', 'jquery', 'common'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        form.on('input-affix(search)', function (data) {
            var elem = data.elem; // 输入框
            if (elem.value === "") {
                layer.msg('请输入搜索内容', {icon: 2, time: 1000});
                return false;
            }
            $.ajax({
                url: '{$Request.root}/Questions/getQuestionsBySearch',
                data: JSON.stringify({keyword: elem.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (res) {
                    if (res.code == 200) {
                        let html = '';
                        if (res.data !== undefined) {
                            for (let i = 0; i < res.data.length; i++) {
                                html += '<option value="' + res.data[i].id + '">' + res.data[i].title + '</option>';
                            }
                        }
                        $("#question").html(html);
                        form.render();
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        form.on('submit(save)', function (data) {
            $.ajax({
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("examination-question-table");
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1000}, function () {
                            common.getToken("{$Request.root}");
                        });
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>